/*
 * Filename    : Entity_Shinta.java
 * Projectname : RamaShinta
 * Date        : Nov 29, 2012 , 4:13:18 PM 
 * 
 */

package com.amikomgamedev.ramashinta.entity;

import java.util.ArrayList;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.ui.activity.BaseGameActivity;

import com.amikomgamedev.ramashinta.ServerData;
import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.game.Game_Data;
import com.amikomgamedev.ramashinta.graphic.Graphic_Anim;
import com.amikomgamedev.ramashinta.graphic.Graphic_Data;
import com.amikomgamedev.ramashinta.graphic.Graphic_Detach;

public class Entity_Shinta implements Graphic_Anim, Graphic_Data
{
	private BaseGameActivity		activity;
	private AnimatedSprite			m_Mc;
	private float					timer;
	private int						second;
	private Graphic_Detach			graphic;

	public ArrayList<Entity_Benda>	listBenda		= new ArrayList<Entity_Benda>();
	
	private int						curState	= -1;
	public int						randomItem = -1;
	private int	keluarinLove = 0;
	private int keluarinChoco = 1;
	public int keluarinRacun = 2;
	private int keluarinKayu = 3;
	private int keluarinApi = 4;
	private int keluarinBoneka = 5;

	public Entity_Shinta(BaseGameActivity pActity, AnimatedSprite p_Mc, Graphic_Detach pGraphic)
	{
		activity = pActity;
		m_Mc = p_Mc;
		switch_State(MC_SHINTA_IDLE);
		timer = 0;
		graphic = pGraphic;

		if (!listBenda.isEmpty())
		{
			listBenda.clear();
		}

	}
	
	

	public void switch_State(int pState)
	{
		curState = pState;
		switch (pState)
		{
			case MC_SHINTA_IDLE:
				m_Mc.animate(MC_SHINTA_DUR[MC_SHINTA_IDLE], MC_SHINTA_FRAME[MC_SHINTA_IDLE], true);
				break;
			case MC_SHINTA_DROP:
				m_Mc.animate(MC_SHINTA_DUR[MC_SHINTA_DROP], MC_SHINTA_FRAME[MC_SHINTA_DROP], 1);
				if(ServerData.getSelectedLevel()==ServerData.LEVEL1){
					randomItem = 0;
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL2){
					randomItem = Utils.GetRandomInRange(0, 1);
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL3){
					randomItem = Utils.GetRandomInRange(0, 3);
					if(randomItem==2){
						randomItem=0;
					}
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL4){
					randomItem = Utils.GetRandomInRange(0, 2);
					if(randomItem==2){
						randomItem=0;
					}
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL5){
					randomItem = Utils.GetRandomInRange(0, 4);
					if(randomItem==3){
						randomItem=0;
					}if(randomItem==4){
						randomItem=1;
					}
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL6){
					randomItem = Utils.GetRandomInRange(0, 4);
					if(randomItem==4){
						randomItem=1;
					}
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL7){
					randomItem = Utils.GetRandomInRange(0, 5);
					if(randomItem==5){
						randomItem=0;
					}
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL8){
					randomItem = Utils.GetRandomInRange(0, 5);
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL9){
					randomItem = Utils.GetRandomInRange(0, 5);
				}else if(ServerData.getSelectedLevel()==ServerData.LEVEL10){
					randomItem = Utils.GetRandomInRange(0, 5);
					if(randomItem==3){
						randomItem=1;
					}
				}
				Utils.TRACE(""+randomItem);
				MengeluarkanRandom(randomItem);
				break;
		}
	}

	int	state_mengeluarkanLove;

	private void MengeluarkanRandom(int rndmItem)
	{
		if(rndmItem==keluarinLove){
		listBenda.add(new Entity_Benda(m_Mc.getX(), m_Mc.getY(), keluarinLove, graphic, activity));
		}else if (rndmItem==keluarinChoco){
		listBenda.add(new Entity_Benda(m_Mc.getX(), m_Mc.getY(), keluarinChoco, graphic, activity));
		}else if(rndmItem==keluarinRacun){
			listBenda.add(new Entity_Benda(m_Mc.getX(), m_Mc.getY(), keluarinRacun, graphic, activity));
		}else if(rndmItem==keluarinKayu){
			listBenda.add(new Entity_Benda(m_Mc.getX(), m_Mc.getY(), keluarinKayu, graphic, activity));
		}else if(rndmItem==keluarinApi){
			listBenda.add(new Entity_Benda(m_Mc.getX(), m_Mc.getY(), keluarinApi, graphic, activity));
		}else if(rndmItem==keluarinBoneka){
			listBenda.add(new Entity_Benda(m_Mc.getX(), m_Mc.getY(), keluarinBoneka, graphic, activity));
		}
	}
	

	public void onUpdate(float pSecondElapsed)
	{
		timer += pSecondElapsed;
		second = (int) timer;
		Utils.TRACE(""+second);
		if (second % 2 == 0)
		{
			if (curState == MC_SHINTA_IDLE)
			{
				switch_State(MC_SHINTA_DROP);
			}
		} else
		{
			switch_State(MC_SHINTA_IDLE);
		}
		for (int i = 0; i < listBenda.size(); i++)
		{
			listBenda.get(i).onUpdate(pSecondElapsed);
			if (listBenda.get(i).sprRandom.getY() > Game_Data.RES_HEIGHT)
			{
				listBenda.remove(i);
			}

		}

	}

}
